package com.ldgame.input;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import com.ldgame.gfx.Screen;

public class Input {
	private static int releasedKey = -1;
	private static int releasedMouseBtn = -1;
	
	private static void listenKeyboard() {
		setReleasedKey(-1);
		while (Keyboard.next()) {
			if (!Keyboard.getEventKeyState()) {
				setReleasedKey(Keyboard.getEventKey());
			}
		}
	}
	
	private static void listenMouse() {
		setReleasedMouseBtn(-1);
		while (Mouse.next()) {
			if (!Mouse.getEventButtonState()) {
				setReleasedMouseBtn(Mouse.getEventButton());
			}
		}		
	}

	private static void setReleasedKey(int _releasedKey) {
		releasedKey = _releasedKey;
	}
	
	private static void setReleasedMouseBtn(int _releasedMouseBtn) {
		releasedMouseBtn = _releasedMouseBtn;
	}

	private static int getReleasedKey() {
		return releasedKey;
	}

	private static int getReleasedMouseBtn() {
		return releasedMouseBtn;
	}
	
	public static void listen() {
		listenKeyboard();	
		listenMouse();
	}
	
	public static boolean checkReleasedKey(int key) {
		return (getReleasedKey() == key);
	}
	
	public static boolean checkPressedKey(int key) {
		return Keyboard.isKeyDown(key);
	}
	
	public static boolean checkReleasedMouseBtn(int mouseButton) {
		return (getReleasedMouseBtn() == mouseButton);
	}
	
	public static boolean checkPressedMouseBtn(int mouseButton) {
		return Mouse.isButtonDown(mouseButton);
	}
	
	public static int getMouseX() {
		return Mouse.getX();
	}
	
	public static int getMouseY() {
		return Screen.getHeight() - Mouse.getY();
	}
}
